package com.awakensoftware.hunterserver.transformers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.awakensoftware.communicationprotocol.contractobjects.treasures.TreasureContractObject;
import com.awakensoftware.hunterserver.serverentities.Treasure;

@Component("treasureTransformer")
public class TreasureTransformer extends TransformerBase<Treasure, TreasureContractObject> {

	@Autowired
	private ChestTransformer chestTransformer;

	@Override
	public Treasure transformFromContractToServerEntity(TreasureContractObject contractObject) {
		Treasure serverEntity = super.tryGetExistingServerEntityById(contractObject.getInternalId());
		if (serverEntity == null) {
			serverEntity = new Treasure();
		}
		super.doCompleteTransformFromContractObjectToServerEntity(contractObject, serverEntity);
		return serverEntity;
	}

	@Override
	public TreasureContractObject transformFromServerEntityToContractObject(Treasure serverEntity) {
		TreasureContractObject contractObject = new TreasureContractObject();
		super.doCompleteTransformFromServerEntityToContractObject(contractObject, serverEntity);
		return contractObject;
	}

	@Override
	protected void doTypeSpecificTransformFromContractObjectToServerEntity(TreasureContractObject contractObject,
			Treasure serverEntity) {
		if (contractObject.getChest() != null) {
			serverEntity.setChest(chestTransformer.transformFromContractToServerEntity(contractObject.getChest()));
		}
		serverEntity.setTreasureMessage(contractObject.getTreasureMessage());
	}

	@Override
	protected void doTypeSpecificTransformFromServerEntityToContractObject(TreasureContractObject contractObject,
			Treasure serverEntity) {
		if (serverEntity.getChest() != null) {
			contractObject
					.setChest(chestTransformer.transformFromServerEntityToContractObject(serverEntity.getChest()));
		}
		contractObject.setTreasureMessage(serverEntity.getTreasureMessage());

	}

}
